package com.wn55.hrms.web.controller;


import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Need;

import com.wn55.hrms.service.NeedService;

import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author z
 */
@RestController
@RequestMapping("need")
@Api(value = "此接口用于操作培训需求功能")
public class NeedController {

    @Autowired
    NeedService needService;


    @GetMapping("/needList")
    @ApiOperation(value = "分页条件查询培训需求")

    public ResultResponse<PageInfo<Need>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now ,
            @RequestParam(value = "size",defaultValue = "2")Integer size,
            String name){
        ResultResponse<PageInfo<Need>> result = null;


        try {
            PageInfo<Need> pageInfo =needService.needList(now,size,name);
            result = new ResultResponse<>(200,"查询需求成功!",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"查询需求失败!");
        }
        return result ;
    }


    @PutMapping("/modify")
    @ApiOperation(value = "修改培训需求")

    public ResultResponse<Void> modify(@RequestBody Need need){
        ResultResponse<Void> result = null;

        try {
            need.setIsDelete(0);

            needService.modify(need);
            result = new ResultResponse<>(200,"修改需求成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"修改需求失败!");
        }
        return result ;
    }



    @PostMapping("/add")
    @ApiOperation(value = "添加培训需求")

    public ResultResponse<Void> add(@RequestBody Need need){
        ResultResponse<Void> result = null;


        try {
            need.setIsDelete(0);

            needService.add(need);
            result = new ResultResponse<>(200,"添加需求成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"添加需求失败!");
        }
        return result ;
    }


    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "删除培训需求")

    public ResultResponse<Void> del(@PathVariable("id") int id){
        ResultResponse<Void> result = null;

        try {
            needService.removeBykey(id);
            result = new ResultResponse<>(200,"删除需求成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(202,"删除需求失败!");
        }
        return result ;
    }


}
